package com.huawei.codelab.utils;

import ohos.app.Context;
import ohos.net.NetHandle;
import ohos.net.NetManager;
import ohos.net.NetStatusCallback;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpRequestUtil {

    private  static String sendRequest(Context context,String urlString,String method,String token,String data){
        String result =null;
        System.out.println("------------------调度成功");
        NetManager netManager = NetManager.getInstance(context);

        if (!netManager.hasDefaultNet()) {
            return null;
        }
        NetHandle netHandle = netManager.getDefaultNet();

// 可以获取网络状态的变化
        NetStatusCallback callback = new NetStatusCallback() {
            // 重写需要获取的网络状态变化的override函数
        };
        netManager.addDefaultNetStatusCallback(callback);

// 通过openConnection来获取URLConnection
        HttpURLConnection connection = null;
        try {
//            String urlString = "http://120.26.194.73/mytest1/login.php"; // 开发者根据实际情况自定义EXAMPLE_URL
            URL url = new URL(urlString);

            connection = (HttpURLConnection)netHandle.openConnection(url, java.net.Proxy.NO_PROXY);
            connection.setRequestMethod(method);
            //如果通过请求题传递参数
            if(data!=null&&("put".equalsIgnoreCase(method)||"post".equalsIgnoreCase(method))){
                //允许通过次网络写入数据到服务器
                connection.setDoOutput(true);

                connection.setRequestProperty("Content-Type","application/json;charset=utf-8");

            }
            //参数token不等于token，将token设置到请求头
            if(token!=null){
                connection.setRequestProperty("token",token);
            }

            connection.connect();
            // 之后可进行url的其他操作
            //如果有data，想后台服务端传递数据
            if(data != null && ("put".equalsIgnoreCase(method) || "post".equalsIgnoreCase(method) ) ){
                byte[] bytes = data.getBytes("UTF-8");
                OutputStream os = connection.getOutputStream();
                os.write(bytes);
                os.flush();
            }

            if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
                System.out.println("进的去-----------");
                InputStream is = connection.getInputStream();
                StringBuilder builder = new StringBuilder();
                byte[] bs =new byte[1024];
                int len = -1;
                while ((len=is.read(bs))!=-1){
                    builder.append(new String(bs,0,len));
                }
                 result=builder.toString();
                return result;
            }

        } catch(IOException e) {
//                    HiLog.error(TAG, "exception happened.");
            e.printStackTrace();
        } finally {
            if (connection != null){
                connection.disconnect();
            }
        }
        return result;
    }

//封装的get
    public static String sendGetRequest(Context context, String urlString){
       return  sendRequest(context,urlString,"GET",null,null);
    }
    public static String sendGetRequestWithToken(Context context, String urlString,String token){
        return  sendRequest(context,urlString,"GET",token,null);
    }

   //封装的delete
    public static String sendDeleteRequest(Context context, String urlString){
        return  sendRequest(context,urlString,"DELETE",null,null);
    }
    public static String sendDeleteRequestWithToken(Context context, String urlString,String token){
        return  sendRequest(context,urlString,"DELETE",token,null);
    }

    //封装的post
    public static String sendPostRequest(Context context, String urlString,String data){
        return  sendRequest(context,urlString,"Post",null,data);
    }
    public static String sendPostRequestWithToken(Context context, String urlString,String token,String data){
        return  sendRequest(context,urlString,"Post",token,data);
    }

    //封装的put
    public static String sendPutRequest(Context context, String urlString,String data){
        return  sendRequest(context,urlString,"PUT",null,data);
    }
    public static String sendPutRequestWithToken(Context context, String urlString,String token,String data){
        return  sendRequest(context,urlString,"PUT",token,data);
    }
    public static String sendPutRequest(Context context, String urlString){
        return  sendRequest(context,urlString,"PUT",null,null);
    }
    public static String sendPutRequestWithToken(Context context, String urlString,String token){
        return  sendRequest(context,urlString,"PUT",token,null);
    }


}
